<?php
	session_start();
	$db = new Model_mdel;
	if(isset($_POST['type'])){
		$uid = $_SESSION['ses_userid'];
		date_default_timezone_set('Asia/Ho_Chi_Minh');
		$name	= $_POST['cart_name'];
		$email	= $_POST['cart_email'];
		$phone	= $_POST['cart_tel'];
		$address = $_POST['cart_address'];
		$message = $_POST['cart_message'];
		$total_price	= $_POST['cart_total'];
		$rule = "/^[a-zA-Z]{1}[a-zA-Z0-9.]{2,20}\@[a-zA-Z0-9]{2,}\.[a-zA-Z]{2,5}$/";
		$rule1 = "/^[0]{1}[0-9]{9,10}$/";
		if($name == "" || $email == "" || $phone == "" || $address == ""){
			echo 1;
			die();
		}else{
			if(preg_match($rule1,$phone)){
				if(preg_match($rule,$email)){
					$data = array(
					"name"	=> $name,
					"email" => $email,
					"phone" => $phone,
					"local" => $address,
					"message"	=> $message,
					"serial" => json_encode($_SESSION['cart']),
					"price" => $total_price,
					"date"	=> date('Y/m/d:H/i/s')
				);
				if(isset($_SESSION['ses_users'])){
					$data['user_id'] = $uid;
				}
				//debug(serialize($_SESSION['cart']));
				sendmail($data);
				$db->insert("tbl_order",$data);
				unset($_SESSION['cart']);
				//session_destroy();
				//echo 4;
				}else{
					echo 2;
				}
			}else{
				echo 3;
			}
		}
	}
	function sendmail($data){
		$config = new Model_mcart;
		$data['info'] = $config->getconfig(1);
		   $mesnger = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                    <html>
                    <head>
                    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                    </head>
                    <body>
                    <h2 style="font-size: 16px;">Đơn hàng từ Shop Online</h2>
                    <h3>Thông tin khách hàng</h3>

            ';
			$mesnger .= "<table><tr><td>Tên khách hàng</td><td>".$data['name']."</td></tr><tr><td>Địa chỉ email</td><td>".$data['email']."</td></tr><tr><td>Điện thoại liên hệ</td><td>".$data['phone']."</td></tr><tr><td>Địa chỉ</td><td>".$data['local']."</td></tr></table>";
			$mesnger .= '<h3>Nội dung đặt hàng</h3>
						<style type="text/css">#bachnx_cart tr td{padding:5px;}</style>
						<table id="bachnx_cart" style="border-collapse: collapse;width: 100%;" border="1" bordercolor="#CCCCCC">
						  <tr style="background-color:#FC0; font-weight:bold">
						    <td style="padding:5px;">STT</td>
						    <td style="padding:5px;">Sản phẩm</td>
						    <td style="padding:5px;">Đơn giá</td>
						    <td style="padding:5px;">Số lượng</td>
						    <td style="padding:5px;">Tổng tiền</td>
						  </tr>
			';
			$unserial = $data['serial'];
		  	$serial = json_decode($unserial,true);
		    $stt = 0;
			foreach($serial as $value){
			$stt++;
			$total = ($value['price']*$value['qty']);
			$mesnger .= '<tr>
						    <td style="padding:5px;">'.$stt.'</td>
						    <td style="padding:5px;"><a href="'.base_url.''.$value['rewrite'].'/p'.$value['id'].'.html"><b>'.$value['name'].'</b></a></td>
						    <td style="padding:5px;">'.@number_format($value['price']).' <u>đ</u></td>
						    <td style="padding:5px;">'.$value['qty'].'</td>
						    <td style="padding:5px;">'.@number_format($total).' <u>đ</u></td>
						  </tr>
			';
			}
			$mesnger .= '<tr class="txt_16">
						    <td class="txt0" colspan=4 align="right" style="padding:5px;"> Tổng tiền </td>
						    <td class="txt_right" style="padding:5px;"> '.@number_format($data['price']).' <u>đ</u></td>
						  </tr>
						</table>
			';
			$mesnger .= '</body></html> ';
			send_mail_helper(''.$data['info']['config_email'].'', 'Thư đặt hàng từ Shop Online', htmlspecialchars_decode($mesnger));
	   }
?>